//
//  KY_ViewController.m
//  ShaQueVideo
//
//  Created by huliang on 13-4-24.
//  Copyright (c) 2013年 科悦信达. All rights reserved.
//

#import "KY_MainViewController.h"
#import <QuartzCore/QuartzCore.h>
#import "KY_VideoListViewController.h"
#import "KY_WeiBoActionSheet.h"
#import "TuijianAppViewController.h"
@interface KY_MainViewController ()

@end

@implementation KY_MainViewController
-(void)viewWillAppear:(BOOL)animated
{
    [self showUpdate];
   // [MBProgressHUD showHUDAddedTo:self.view animated:YES];

    NSURL *url = [NSURL URLWithString:URL_INDEX_LIST];
    self.dataRequest = [[ASIFormDataRequest alloc] initWithURL:url];
    NSDictionary *timeDictionary = [[NSUserDefaults standardUserDefaults] objectForKey:@"newTime"];
    NSString *valueString = [timeDictionary JSONString];

    
    [self.dataRequest setPostValue:valueString forKey:@"cid_last_clicked_times"];
    [self.dataRequest setDelegate:self];
    [self.dataRequest startAsynchronous];
    
}
-(void)viewWillDisappear:(BOOL)animated
{
    [self.dataRequest cancel];
    [self.dataRequest setDelegate:nil];
     self.dataRequest = nil;

}

-(IBAction)imageTouch:(id)sender
{
    UIControl *control = (UIControl *)sender;
    switch (control.tag) {
        case 13:
            [self showtheDetailList:[NSString stringWithFormat:@"%d",13]];
            break;
        case 14:
             [self showtheDetailList:[NSString stringWithFormat:@"%d",14]];
            break;
        case 15:
            [self showtheDetailList:[NSString stringWithFormat:@"%d",15]];
            break;

        case 16:
            [self showtheDetailList:[NSString stringWithFormat:@"%d",16]];
            
            break;

        case 17:
            [self showtheDetailList:[NSString stringWithFormat:@"%d",17]];
            break;
        case 18:
            [self showtheDetailList:[NSString stringWithFormat:@"%d",18]];
            break;
            
        default:
            break;
    }
}
-(void)showUpdate
{
    NSDictionary *updateDic = [[NSUserDefaults standardUserDefaults] objectForKey:@"update"];

    NSArray *keyArry = [updateDic allKeys];

    for (NSString *str in keyArry) {

        NSInteger k = [str intValue];
        NSString *upStr = [updateDic objectForKey:str];

        switch (k) {

            case 13:
                if (![upStr isEqualToString:@"0"]) {

                    self.beijuLabel.hidden = NO;
                    self.beijuLabel.text = upStr;
                }
                else
                {
                    self.beijuLabel.hidden = YES;

                }
                break;
            case 14:

                if (![upStr isEqualToString:@"0"]) {

                    self.diaobaoLabel.hidden = NO;
                    self.diaobaoLabel.text = upStr;
                }
                else
                {
                    self.diaobaoLabel.hidden = YES;

                }

                break;
            case 15:
                if (![upStr isEqualToString:@"0"]) {

                    self.meinvLabel.hidden = NO;
                    self.meinvLabel.text = upStr;
                }
                else
                {
                    self.meinvLabel.hidden = YES;
                }
                break;
            case 16:
                if (![upStr isEqualToString:@"0"]) {

                    self.childLabel.hidden = NO;
                    self.childLabel.text = upStr;
                }
                else
                {
                    self.childLabel.hidden = YES;
                }
                break;
            case 17:
                if (![upStr isEqualToString:@"0"]) {

                    self.egaoLabel.hidden = NO;
                    self.egaoLabel.text = upStr;
                }
                else
                {
                    self.egaoLabel.hidden = YES;

                }
                break;
            case 18:
                if (![upStr isEqualToString:@"0"]) {

                    self.animalLabel.hidden = NO;
                    self.animalLabel.text = upStr;
                }
                else
                {
                    self.animalLabel.hidden = YES;
                }
                break;
            default:
                break;
        }
    }

}
-(void)showTuijianPage:(id)sender
{
    TuijianAppViewController *viewController = [[TuijianAppViewController alloc] initWithNibName:@"TuijianAppViewController" bundle:nil];
    [self.navigationController pushViewController:viewController animated:YES];
    [viewController release];
}
- (void)viewDidLoad
{

    [super viewDidLoad];
    
    self.navigationItem.title = @"傻缺视频";
    UIButton *recomandBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    [recomandBtn setFrame:CGRectMake(0, 0, 53, 23)];
    [recomandBtn addTarget:self action:@selector(showTuijianPage:) forControlEvents:UIControlEventTouchUpInside];
    [recomandBtn setImage:[UIImage imageNamed:@"rightbtn"] forState:UIControlStateNormal];
    UIBarButtonItem *recomandItem = [[[UIBarButtonItem alloc] initWithCustomView:recomandBtn] autorelease];
    self.navigationItem.rightBarButtonItem = recomandItem;
    UILabel *label = (UILabel *)[self.view viewWithTag:100];
    
    label.backgroundColor = [UIColor colorWithPatternImage:[UIImage imageNamed:@"have_new.png"]];
    
    self.categoryArr = [NSArray array];
    
    [self.diaobaoImage setcorner];
    [self.beijuImage setcorner];
    [self.meinvImage setcorner];
    [self.childImage setcorner];
    [self.animalImage setcorner];
    [self.egaoImage setcorner];

    [self.diaobaoLabel setBackgroundColor:[UIColor colorWithPatternImage:[UIImage imageNamed:@"have_new.png"]]];
    [self.beijuLabel setBackgroundColor:[UIColor colorWithPatternImage:[UIImage imageNamed:@"have_new.png"]]];
    [self.meinvLabel setBackgroundColor:[UIColor colorWithPatternImage:[UIImage imageNamed:@"have_new.png"]]];
    [self.childLabel setBackgroundColor:[UIColor colorWithPatternImage:[UIImage imageNamed:@"have_new.png"]]];
    [self.animalLabel setBackgroundColor:[UIColor colorWithPatternImage:[UIImage imageNamed:@"have_new.png"]]];
    [self.egaoLabel setBackgroundColor:[UIColor colorWithPatternImage:[UIImage imageNamed:@"have_new.png"]]];
    
}

- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
}

-(void)showtheDetailList:(NSString *)cid{

    KY_VideoListViewController *videoList =  [[KY_VideoListViewController alloc] initWithNibName:@"KY_VideoListViewController" bundle:nil];
    videoList.cid = cid;
    [videoList loadData];
    [self.navigationController pushViewController:videoList animated:YES];
    [videoList release];
}
#pragma ----
-(void)requestFinished:(ASIHTTPRequest *)request
{
    // [MBProgressHUD hideAllHUDsForView:self.view animated:YES];
      NSString *response = [request responseString];
      NSDictionary *resultDictionary = [response objectFromJSONString];
      NSLog(@"%@",response);

    NSArray *keyArray = [resultDictionary allKeys];
    NSDictionary *updateDic = [[NSUserDefaults standardUserDefaults] objectForKey:@"update"];
    NSMutableDictionary *m_dic = [NSMutableDictionary dictionaryWithDictionary:updateDic];
    int totalAppNum = 0;
    int totalAppNewNum = 0;
    for (NSString*str in keyArray) {

        NSDictionary *dic = [resultDictionary objectForKey:str];
        NSInteger valueNum = [str intValue];
        switch (valueNum) {
            case 13:
            {
               
                [m_dic removeObjectForKey:str];
                [m_dic setObject:[dic objectForKey:@"new_num"] forKey:str];
                totalAppNum += [[dic objectForKey:@"new_num"] intValue];
                UILabel *label = (UILabel *)[self.view viewWithTag:113];
                label.text = [NSString stringWithFormat:@"杯具了(%@)",[dic objectForKey:@"total_num"]];
                totalAppNum += [[dic objectForKey:@"total_num"] intValue];
            }
                break;
            case 14:
            {
                [m_dic removeObjectForKey:str];
                [m_dic setObject:[dic objectForKey:@"new_num"] forKey:str];
                totalAppNum += [[dic objectForKey:@"new_num"] intValue];
                UILabel *label = (UILabel *)[self.view viewWithTag:114];
                label.text = [NSString stringWithFormat:@"屌爆了(%@)",[dic objectForKey:@"total_num"]];
                totalAppNum += [[dic objectForKey:@"total_num"] intValue];
            }
                break;
            case 15:
            {
                [m_dic removeObjectForKey:str];
                [m_dic setObject:[dic objectForKey:@"new_num"] forKey:str];
                totalAppNum += [[dic objectForKey:@"new_num"] intValue];
                UILabel *label = (UILabel *)[self.view viewWithTag:115];
                label.text = [NSString stringWithFormat:@"傻缺美女(%@)",[dic objectForKey:@"total_num"]];

                totalAppNum += [[dic objectForKey:@"total_num"] intValue];

                
            }
                break;
            case 16:
            {
                [m_dic removeObjectForKey:str];
                [m_dic setObject:[dic objectForKey:@"new_num"] forKey:str];
                totalAppNum += [[dic objectForKey:@"new_num"] intValue];
                UILabel *label = (UILabel *)[self.view viewWithTag:116];
                label.text = [NSString stringWithFormat:@"傻缺小孩(%@)",[dic objectForKey:@"total_num"]];

                NSLog(@"child:%@",[dic objectForKey:@"total_num"]);

//                NSLog(@"child:%@",[dic objectForKey:@"total_num"]);
                totalAppNum += [[dic objectForKey:@"total_num"] intValue];
            }
                break;
            case 17:
            {
                [m_dic removeObjectForKey:str];
                [m_dic setObject:[dic objectForKey:@"new_num"] forKey:str];
                totalAppNum += [[dic objectForKey:@"new_num"] intValue];
                UILabel *label = (UILabel *)[self.view viewWithTag:117];
                label.text = [NSString stringWithFormat:@"恶搞(%@)",[dic objectForKey:@"total_num"]];
                totalAppNum += [[dic objectForKey:@"total_num"] intValue];
            }
                break;
            case 18:
            {
                [m_dic removeObjectForKey:str];
                [m_dic setObject:[dic objectForKey:@"new_num"] forKey:str];
                totalAppNum += [[dic objectForKey:@"new_num"] intValue];
                UILabel *label = (UILabel *)[self.view viewWithTag:118];
                label.text = [NSString stringWithFormat:@"傻缺动物(%@)",[dic objectForKey:@"total_num"]];
                totalAppNum += [[dic objectForKey:@"total_num"] intValue];
            }
                break;
            default:
                break;
        }
    }
    //更新标题和app icon badge ， 标示数量
    if(totalAppNum > 0) self.title = [NSString stringWithFormat:@"傻缺视频（%d）",totalAppNum];
    if(totalAppNewNum > 0) [UIApplication sharedApplication].applicationIconBadgeNumber = totalAppNewNum;
    
    UITabBarItem *tabBarItem;
    tabBarItem = [[UITabBarItem alloc] initWithTitle:@"傻缺视频" image:[UIImage imageNamed:@"index_normal"] tag:1];
    [tabBarItem setFinishedSelectedImage:[UIImage imageNamed:@"index_selected"] withFinishedUnselectedImage:[UIImage imageNamed:@"index_normal"]];
    self.navigationController.tabBarItem = tabBarItem;
    [tabBarItem release];
    
    [[NSUserDefaults standardUserDefaults] setObject:m_dic forKey:@"update"];
    [[NSUserDefaults standardUserDefaults] synchronize];
    
    [self showUpdate];
}
-(void)requestFailed:(ASIHTTPRequest *)request
{
     [MBProgressHUD hideAllHUDsForView:self.view animated:YES];
}
#pragma ------- offlinedelegate

// 积分墙加载完成。此方法实现中可进行积分墙入口Button显示等操作。
// Load offer wall successfully. You can set your IBOutlet.hidden to NO in this callback.
// This IBOutlet is the one which response to present OfferWall.



@end
